package entreprise;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import util.P;

public class Email {
	private String user_name;
	private String password;
	private String user_name_send;
	private String user_name_recive;
	private String content;
	private String subject;

	public Email() {
		user_name = P.username;
		password = P.password;
		user_name_send = null;
		user_name_recive = null;
		content = null;
		subject = null;
	}
	
	public Email(String user_name, String password) {
		this.user_name = user_name;
		this.password = password;
		user_name_send = null;
		user_name_recive = null;
		content = null;
		subject = null;
	}

	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUser_name_send() {
		return user_name_send;
	}

	public void setUser_name_send(String user_name_send) {
		this.user_name_send = user_name_send;
	}

	public String getUser_name_recive() {
		return user_name_recive;
	}

	public void setUser_name_recive(String user_name_recive) {
		this.user_name_recive = user_name_recive;
	}

	public boolean sendMail() {
		if (this.user_name_send == null || this.user_name_recive == null)
			return false;

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(user_name, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(this.user_name_send));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(this.user_name_recive));
			message.setSubject(this.subject);
			message.setText(this.content);
			Transport.send(message);
			return true;

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		Email mail = new Email();
		mail.setUser_name_recive("nqkhai1706@gmail.com");
		mail.setUser_name_send("nguyenkhair@gmail.com");
		mail.setSubject("test");
		mail.setContent("Hello");
		mail.sendMail();
		
	}
}